<?php
namespace common\services;

use common\helpers\TStringHelper;
use common\models\ExhibitionCategory;
use common\models\Region;
use common\models\User;
use common\models\UserAccount;
use common\models\UserMessage;
use yii\data\Pagination;

class UserServices extends User
{

    /**
     * 会员查询
     * @param $params
     * @return array
     */
    public function search($params){
        $model = User::find();

        if (isset($params['status'])) {
            $model->andFilterWhere(['=', 'status', $params['status']]);
        }

        if (isset($params['recommand_username'])) {
            $model->andFilterWhere(['=', 'recommand_username', $params['recommand_username']]);
        }

        $count = $model->count(1);
        if ($count > 0){
            $page = new Pagination(['defaultPageSize' => $params['defaultPageSize'], 'totalCount' => $count]);
            $data = $model->orderBy('created_at desc')->offset($page->offset)->limit($page->limit)->asArray()->all();
        }else{
            $page = $data = null;
        }
        return ['data' => $data, 'page' => $page];
    }

    /**
     * 用户留言
     * @param $params
     * @return array
     */
    public function searchUserMessage($params){
        $model = UserMessage::find();
        if (isset($params['user_id'])){
            $model->andFilterWhere(['=', 'user_id', $params['user_id']]);
        }
        if (isset($params['status'])) {
            $model->andFilterWhere(['=', 'status', $params['status']]);
        }
        if (isset($params['reply_status'])) {
            $model->andFilterWhere(['=', 'reply_status', $params['reply_status']]);
        }

        $count = $model->count(1);
        if ($count > 0){
            $page = new Pagination(['defaultPageSize' => $params['defaultPageSize'], 'totalCount' => $count]);
            $data = $model->orderBy('created_at desc')->offset($page->offset)->limit($page->limit)->asArray()->all();
        }else{
            $page = $data = null;
        }
        return ['data' => $data, 'page' => $page];
    }

    //账号安全等级
    public function getAccountSafetyGrade($user) {
        $safety_score = $user->safety_score;
        $total = 0;
        //密码安全等级
        if ($safety_score == 2) {
            $total = 25;
        }elseif ($safety_score == 3) {
            $total = 50;
        }
        //手机验证
        if ($user->is_mobile_verify) {
            $total += 25;
        }
        //邮箱验证
        if ($user->is_email_verify) {
            $total += 25;
        }
        return $total;
    }

    /**
     * @param $data
     * @param $model
     * @return array
     */
    public function updateBasic($data, $model){
        $xss = "/[\'.,:;*?~`!@#$%^&+=)(<>{}]|\]|\[|\/|\\\|\"|\|/";
        //昵称
        if (!empty($data['nickname'])) {
            if (preg_match($xss, $data['nickname'])) {
                return ['code' => -1, 'data' => '', 'msg' => '昵称包含有特殊字符'];
            }
            $model->nickname = $data['nickname'];
        }else{
            return ['code' => -1, 'data' => '', 'msg' => '昵称不能为空'];
        }
        //个性签名
        if (!empty($data['signature'])) {
            $model->signature = $data['signature'];
        }
        //真实姓名
        if (!empty($data['realname'])) {
            if (preg_match($xss, $data['realname'])) {
                return ['code' => -1, 'data' => '', 'msg' => '真实姓名包含有特殊字符'];
            }
            $model->realname = $data['realname'];
        }else{
            return ['code' => -1, 'data' => '', 'msg' => '真实姓名不能为空'];
        }
        if (!empty($data['mobile'])) {
            if ($model->mobile != $data['mobile']) {
                if (!TStringHelper::isMobile($data['mobile'])) {
                    return ['code' => -1, 'data' => '', 'msg' => '手机格式错误'];
                }
                if ($this->checkField('mobile', $data['mobile'], $model->id)) {
                    return ['code' => -1, 'data' => '', 'msg' => '此手机号禁止注册'];
                }
                $model->mobile = $data['mobile'];
                $model->is_mobile_verify = 0;
            }
        }else{
            return ['code' => -1, 'data' => '', 'msg' => '手机不能为空'];
        }
        if (!empty($data['email'])) {
            if ($model->email != $data['email']) {
                if (!TStringHelper::isEmail($data['email'])) {
                    return ['code' => -1, 'data' => '', 'msg' => '邮箱格式错误'];
                }
                if ($this->checkField('email', $data['email'], $model->id)) {
                    return ['code' => -1, 'data' => '', 'msg' => '此邮箱禁止注册'];
                }
                $model->email = $data['email'];
                $model->is_email_verify = 0;
            }
        }else{
            return ['code' => -1, 'data' => '', 'msg' => '邮箱不能为空'];
        }
        if (!empty($data['city'])) {
            $city = '';
            $region = Region::findOne($data['city']);
            $pid = $region->pid;
            if ($pid > 0) {
                $parents = Region::findOne($pid);
                $city .= 'province,'.$pid.','.$parents->name.'|||';
            }
            $city .= 'city,'.$data['city'].','.$region->name;
            $model->city = $city;
        }else{
            return ['code' => -1, 'data' => '', 'msg' => '所在城市不能为空'];
        }
        if (!empty($data['address'])) {
            $model->address = $data['address'];
        }
        if (!empty($data['follow_industry'])) {
            $category = ExhibitionCategory::findOne($data['follow_industry']);
            $model->follow_industry = $category->name;
        }else{
            return ['code' => -1, 'data' => '', 'msg' => '关注行业不能为空'];
        }
        if ($model->update() !== false) {
            return ['code' => 0, 'data' => '', 'msg' => 'ok'];
        }else{
            return ['code' => -1, 'data' => '', 'msg' => '保存失败'];
        }
    }

    /**
     * 检测是否存在相同字段的值
     * @param $field
     * @param $value
     * @param string $id
     * @return bool
     */
    public function checkField($field, $value, $id = '') {
        $model = User::find(false);
        if (!empty($id)) {
            $model->andFilterWhere(['<>', 'id', $id]);
        }
        $model->andFilterWhere(['=', "$field", $value]);
        $count = $model->count(1);
        return intval($count) > 0 ? true : false;
    }

}
